<?php

namespace App\Http\Controllers;

use App\Events\WechatSubscribe;
use App\Interfaces\DistributeInterface;
use App\Interfaces\UserInterface;
use App\Repositories\CouponRepository;
use App\User;
use App\UserLeader;
use Illuminate\Http\Request;

class WechatController extends Controller
{
    protected $user;
    protected $distribute;

    public function __construct(UserInterface $user, DistributeInterface $distribute)
    {

        $this->user = $user;
        $this->distribute = $distribute;
    }

    public function serve()
    {

        \Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志
        $app = app('wechat.official_account');


        $app->server->push(function ($message) {
            // \Log::info('11',$message);

            switch ($message['MsgType']) {
                case 'event':
                    return $this->event($message);
                    break;
                case 'text':
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });

        return $app->server->serve();
    }

    protected function event($message)
    {

        if ($message['Event'] == "SCAN") {//关注
            $user = User::where('openid', $message['FromUserName'])->first();
            if (!$user->leader) {
                $userLeader = UserLeader::where('user_id', $user->id)->first();
                if ($userLeader) {
                    $leader = $this->distribute->bindDistribute($user, $userLeader->leader_id);
                    $str = "欢迎您关注【加点幸福】,开启幸福之旅!\n您的会员编号为{$user->id}\n您的分享人为  {$leader->name}\n成为商城正式会员后，享受复购八折优惠！\n点击下方进入【幸福商城】吧.";
                    return $str;
                }
            }
            //\Log::info('到达user');
            $userInfo = $this->user->createWechatUserByOpenid($message['FromUserName']);
            //\Log::info('userinfo',$userInfo->toArray());
            if (isset($message['EventKey'])) {//新创建用户并且二维码带参数 执行分销
                $leader = $this->distribute->bindDistribute($userInfo, $message['EventKey']);
                $str = "欢迎您关注【加点幸福】,开启幸福之旅!\n您的会员编号为{$userInfo->id}\n您的分享人为  {$leader->name}\n成为商城正式会员后，享受复购八折优惠！\n点击下方进入【幸福商城】吧.";
               return $str;
            }
        } elseif ($message['Event'] == "subscribe") {//普通关注 未关注关注事件
            $user = User::where('openid', $message['FromUserName'])->first();
            $userInfo = $this->user->createWechatUserByOpenid($message['FromUserName']);
            if (!$user) {
                //触发新用户关注事件

                $str = "欢迎您关注【加点幸福】,开启幸福之旅!\n您的会员编号为{$userInfo->id}\n您的分享人为  商城平台\n成为商城正式会员后，享受复购八折优惠！\n点击下方进入【幸福商城】吧.";
                return $str;
            } else {

                //如果没有leader
                if (!$user->leader) {
                    $userLeader = UserLeader::where('user_id', $user->id)->first();
                    if ($userLeader) {
                        $leader = $this->distribute->bindDistribute($user, $userLeader->leader_id);
                        $str = "欢迎您关注【加点幸福】,开启幸福之旅!\n您的会员编号为{$user->id}\n您的分享人为  {$leader->name}\n成为商城正式会员后，享受复购八折优惠！\n点击下方进入【幸福商城】吧.";
                        return $str;
                    }
                }
            }
            $str = "欢迎您关注【加点幸福】,开启幸福之旅!\n您的会员编号为{$userInfo->id}\n您的分享人为  商城平台\n成为商城正式会员后，享受复购八折优惠！\n点击下方进入【幸福商城】吧.";
            return $str;
        }

    }

    public function createMenu()
    {
        $app = app('wechat');
        //$url = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        $buttons = [
            [
                "name" => "微网站",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "景区介绍",
                        "url" => route('wap.about')
                    ],
                    [
                        "type" => "view",
                        "name" => "旅游攻略",
                        "url" => route('wap.post-list', ['slug' => 'lygl'])
                    ]
                ],
            ],
            [
                "name" => "景点讲解",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "在线讲解",
                        "url" => route('shake')
                    ],
                    [
                        "type" => "view",
                        "name" => "讲解试听",
                        "url" => route('scene_index')
                    ]
                ],
            ],
            [
                "name" => "个人中心",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "老虎机",
                        "url" => url('game/tiger')
                    ],
                    [
                        "type" => "view",
                        "name" => "购买套餐",
                        "url" => route('combo_list')
                    ],
                    [
                        "type" => "view",
                        "name" => "促销活动",
                        "url" => route('activity')
                    ],
                    [
                        "type" => "view",
                        "name" => "我的推广",
                        "url" => route('user_group')
                    ],
                    [
                        "type" => "view",
                        "name" => "我的信息",
                        "url" => route('user')
                    ],
                ],
            ],
        ];
        $app->menu->add($buttons);
    }
}
